// ActionScript file
import com.senocular.display.*;

import flash.display.Bitmap;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;

import mx.collections.ArrayCollection;
import mx.containers.Accordion;
import mx.containers.Canvas;
import mx.controls.Label;
import mx.controls.List;
import mx.effects.Zoom;
import mx.events.ItemClickEvent;
import mx.events.ListEvent;
import mx.events.NumericStepperEvent;
import mx.events.SliderEvent;
import mx.managers.PopUpManager;
import mx.managers.SystemManager;

public var ttool:TransformTool = new TransformTool();


[Bindable] public var loadedSM:SystemManager;


public var zoom:Zoom = new Zoom();  
public var mling:myloading = new myloading(); 
public var ishowml:Boolean = false;


// 从载入的应用程序获得信息，初始化变量。
public function sladdtostage(event):void{
	trace('added to stage');
	loadedSM = SystemManager(sl.content);
	canvas1(loadedSM.application).setApp(this);//设置加载程序的handle
	
	//设置默认画布大小800x600
	var obj:Object = {width:this.default_w, height:this.default_h};
	canvas1(loadedSM.application).setCanvaSize(obj);
	
	//缩放画布到70%
	zoom.zoomHeightFrom = this.hsz_zoomHfrom;
	zoom.zoomWidthFrom = this.hsz_zoomWfrom;
	zoom.zoomHeightTo = 0.7;  
	zoom.zoomWidthTo = 0.7;
	this.hsz_zoomHfrom = 0.7;
	this.hsz_zoomWfrom = 0.7;  
	zoom.play();
	hsz.value = 70;
	
	if(this.sw!=0 && this.sh!=0){
		var pxw:Number = Math.round(sw * 2.836);
		var pxh:Number = Math.round(sh * 2.836);
		var cla:String = pxw+'*'+pxh+' 像素';
		var obj:Object = {width:pxw, height:pxh, cl:cla};
		this.SetCanvas2(obj);
	}
	
	
	//swfloader加载完毕，删除初始化时进度条 1
	this.initTotal -= 1;
	if(this.initTotal == 0){			
		//this.removeChild(this.mling);
		this.mling.visible = false;
	}
}


/** 
 * complete
 * 
 */
public function comp():void{
	trace('application creation Complete');
	//this.swfloaderInit();
	
	this.userid = Application.application.parameters["userid"];
	this.shopid = Application.application.parameters["sid"];
	this.sessionid = Application.application.parameters["seid"];
	this.sw = Application.application.parameters["w"];
	this.sh = Application.application.parameters["h"];

	fileList.addEventListener(Event.ACTIVATE, fileActivate);
    fileList.addEventListener(Event.SELECT, fileSelect);
    fileList.addEventListener(Event.DEACTIVATE, fileDeactivate);
    fileList.addEventListener(Event.CANCEL, fileCancel);





	// 初始化右边栏 我的照片
	//MyPhotoRPC.send();
	mling.setLab('加载配置文件.');
	this.SendHttpService(this.URLConfigRPC, null, this.getConfigRPCResult, 'Photo');
	this.SendHttpService(this.URLgetVersionRPC, null, this.getVersionRPCResult, 'Photo');
}



/**
 * 初始化程序
 */
public function ini():void{
	//初始化时显示进度条
	mling.mylbild();
	addChild(this.mling);
	
	// 初始的左边栏
	brightness_value = 0;
	contrast_value = 0;
	saturation_value = 0;
	hue_value = 0;
	left_box.removeAllChildren();
	left_box.addChild(effectControls());
	//original.enabled = false;
	zoom.target = sl;
}

/**
 * zoom out
 */
public function zout():void{
	var tmp:Number = hsz.value;
	var tmp1:Number = (tmp+1)/100; //hsz的值是1到100。需转成0.01到1。
	//设定from的值，不然会从0.01开始；只因有地方设过from，它会沿用设过的值。
	zoom.zoomHeightFrom = this.hsz_zoomHfrom;
	zoom.zoomWidthFrom = this.hsz_zoomWfrom;
	zoom.zoomHeightTo = tmp1;  
	zoom.zoomWidthTo = tmp1;
	this.hsz_zoomHfrom = tmp1;
	this.hsz_zoomWfrom = tmp1;  
	zoom.play();
	hsz.value = tmp+1;
}

/**
 * zoom in
 */
public function zin():void{
	var tmp:Number = hsz.value;
	var tmp1:Number = (tmp-1)/100;
	zoom.zoomHeightFrom = this.hsz_zoomHfrom;
	zoom.zoomWidthFrom = this.hsz_zoomWfrom;
	// 目标增加高度倍数  
	zoom.zoomHeightTo = tmp1;  
	// 目标增加宽度倍数  
	zoom.zoomWidthTo = tmp1;
	this.hsz_zoomHfrom = tmp1;
	this.hsz_zoomWfrom = tmp1;
	// 开始  
	zoom.play();
	hsz.value = tmp-1;
}

public function zo():void{
	zoom.zoomHeightFrom = this.hsz_zoomHfrom;
	zoom.zoomWidthFrom = this.hsz_zoomWfrom;
	zoom.zoomHeightTo = 1;  
	zoom.zoomWidthTo = 1;  
	zoom.play();
	this.hsz_zoomHfrom = 1;
	this.hsz_zoomWfrom = 1;
	hsz.value = 100;
}

private function a11():void{  
	zoom.zoomHeightFrom = this.hsz_zoomHfrom;
	zoom.zoomWidthFrom = this.hsz_zoomWfrom;
	zoom.zoomHeightTo = 1;  
	zoom.zoomWidthTo = 1;  
	zoom.play();
	hsz.value = 100;
	this.hsz_zoomHfrom = 1;
	this.hsz_zoomWfrom = 1;
}  


protected function zoomsl(event:SliderEvent):void
{
	var val:Number = hsz.value/100;
	//trace(val);
	zoom.zoomHeightFrom = this.hsz_zoomHfrom;
	zoom.zoomWidthFrom = this.hsz_zoomWfrom;
	zoom.zoomHeightTo = val;
	zoom.zoomWidthTo = val;
	this.hsz_zoomHfrom = val;
	this.hsz_zoomWfrom = val;
	zoom.play();
}

//完成设计
public function finishDesign():void{
	canvas1(loadedSM.application).setStyle('backgroundAlpha', '0');
	canvas1(loadedSM.application).ttool.target = null;
	canvas1(loadedSM.application).finish(currentTabIndex, isOPT);
}
//完成设计,存成文件，并记入数据库
public function doFinishDesign(data:ByteArray):void{
	Alert.show('您选择了'+this.default_name+'的画布尺寸，超出此范围的内容将被裁切。', '注意', 0x4, pa1);
	
	//生成图片文件
	var byteArray:ByteArray = data;
	
	var header:URLRequestHeader = new URLRequestHeader("Content-type", "applicaton/octet-stream");
	var tmpurl:String = new String();
	tmpurl = "finishdesign.php?size="+this.default_w+'*'+this.default_h+"&goods_id="+this.shopid;
	var savePNG:URLRequest = new URLRequest(tmpurl);
	savePNG.requestHeaders.push(header);
	savePNG.method = URLRequestMethod.POST;
	savePNG.data = byteArray;
	var l:URLLoader = new URLLoader();  
	l.load(savePNG);
	l.addEventListener(Event.COMPLETE, function (e:Event):void{finishDesignComplete(e);});
}
//完成设计生成文件回调
private function finishDesignComplete(e:Event):void{
	
	var l:URLLoader = URLLoader(e.target);
	trace(l.data);
	//Alert.show(l.data, 'DEBUG', 0x4, pa1);
	var ld:XML = XML(l.data);
	if(ld.info.result == 'OK'){
		this.SendHttpService(this.url_MyMakedRPC, null,this.MakedRPCResult, 'Finish Design');
	}
	//removeProcessBar();
}



/**
 * 我的照片双击事件
 */
private function myPDClick(e:ListEvent):void{
		
	switch(currentTabIndex){
		case 0:
			// 特效事件，对照片处理。
			//onEffect(e.itemRenderer.data);
		case 1: //场景
		case 2: //边框
		case 3: //饰品
		case 4: //文字
		case 5:
			//onEffect(e.itemRenderer.data);
			var value:Object = e.itemRenderer.data;
			this.oriPhoto = e.itemRenderer.data;
			canvas1(loadedSM.application).addPhoto(value.urlx, value.width, value.height);
			//original.enabled = true; //“原图”按钮启用。
			break;
		default :
			trace('myPDClick is default');
			break;
	}
	
}





/**
 * 功能选择，tab点击的事件
 */
private function funSelect(event:ItemClickEvent):void{
	
	//当前tab的index
	currentTabIndex = event.index;
	switch (event.index)
	{
		case 0:
			//processFlag = 'effect'; // set process flag
			//取消拖拽
			id_sources.dragEnabled = false;
			id_sources.dropEnabled = false;
			
			myar=new ArrayCollection([
				{name: "bw", urlx: SITEURL + "fm/assets/make/bw.png"}, 
				{name: "blur", urlx: SITEURL + "fm/assets/make/blur.png"}, 
				{name: "invert", urlx: SITEURL + "fm/assets/make/invert.png"}, 
				{name: "old", urlx: SITEURL + "fm/assets/make/old.png"}, 
				{name: "emboss", urlx: SITEURL + "fm/assets/make/emboss.png"}				 
				]);
			left_box.removeAllChildren();
			left_box.addChild(effectControls());
			doleft.selectedIndex = 0; //设置默认选中素材栏
			break;
		case 1:
			createAccMenu('stage');
			myar = new ArrayCollection();
			//启用拖拽
			id_sources.dragEnabled = true;
			id_sources.dropEnabled = false;
			doleft.selectedIndex = 0;
			break;
			
		case 2:
			createAccMenu('border');
			myar = new ArrayCollection();
			//启用拖拽
			id_sources.dragEnabled = true;
			id_sources.dropEnabled = false;
			doleft.selectedIndex = 0;
			break;
			
		case 3:
			createAccMenu('jewelry');
			myar = new ArrayCollection();
			//启用拖拽
			id_sources.dragEnabled = true;
			id_sources.dropEnabled = false;
			doleft.selectedIndex = 0;
			break;
		
		case 4:
			left_box.removeAllChildren();
			left_box.addChild(texttool());
			myar = new ArrayCollection();
			//启用拖拽
			id_sources.dragEnabled = true;
			id_sources.dropEnabled = false;
			doleft.selectedIndex = 0;
			break;
		
		case 5:
			createAccMenu('calendar');
			myar = new ArrayCollection();
			//启用拖拽
			id_sources.dragEnabled = true;
			id_sources.dropEnabled = false;
			doleft.selectedIndex = 0;
			break;
			
		default:
			break;
	}
	
}

/**
 * 右部效果预览中的素材点击事件
 */
private function sourceclick(event:ListEvent):void{
	switch(currentTabIndex){
		case 0 :
			//trace(event.itemRenderer.data);
			canvas1(loadedSM.application).addEffect(event.itemRenderer.data.name);
			this.isOPT = 4;
			break;
		case 1 : //场景
			this.click_obj_1 = event.itemRenderer.data; //先保存点击取得的数据，即素材的信息.
			this.click_obj_1_string = '1';
			trace(click_obj_1);
			
			canvas1(loadedSM.application).addSomething(event.itemRenderer.data, 'stage');
			
			//SetStage(this);
			this.isOPT = 1;
			
			break;
		case 2 : //边框
									
			trace('isOPT='+this.isOPT);
			this.click_obj_1 = event.itemRenderer.data; //先保存点击取得的数据，即素材的信息.
			this.click_obj_1_string = '1';
			//mtab.enabled = false;
			//若isOPT=2,3,4；则先执行合成。
			//if(makeMerge(this.isOPT, currentTabIndex) == 0){
				canvas1(loadedSM.application).add_Border_(event.itemRenderer.data, 'border');
			//}
			this.isOPT = 1;
			break;
		case 3 : //饰品
			canvas1(loadedSM.application).addJewelry(event.itemRenderer.data);
			this.isOPT = 2;
			break;
		case 4 : //文字
			//doEffect(event.itemRenderer.data.name);
			this.isOPT = 3;
			break;
		case 5 : //日历
			this.click_obj_1 = event.itemRenderer.data; //先保存点击取得的数据，即素材的信息.
			this.click_obj_1_string = '1';
			canvas1(loadedSM.application).addSomething(event.itemRenderer.data, 'calendar');
			this.isOPT = 1;
			break;
		default :
			break;
	}

	trace('确认点击事件：');trace(this.isOPT);//确认拖拉事件前是否先执行点击事件
}




/**
 * 已经完成作品的双击事件
 */
public function makedClick(e:ListEvent):void{
	//onEffect(e.itemRenderer.data);
	//var tmp:Object = e.itemRenderer.data;
	//canvas1(loadedSM.application).addPhoto(tmp.urlx, tmp.width, tmp.height);
}


/**
 * 创建左则素材菜单
 */
private function createAccMenu(value:String):void{
	var menuxml:XML;
	switch(value){
		case 'stage':
			menuxml = stageXML;
			break;
		case 'border':
			menuxml = borderXML;
			break;
		case 'jewelry':
			menuxml = jewelryXML;
			break;
		case 'calendar':
			menuxml = calendarXML;
			break;	
		default:
			break;
	}
	
	var menu1:Accordion = new Accordion();
	//menu1.percentHeight = 100;
	menu1.width = 166;
	menu1.setStyle('backgroundColor', '#eefdfc');
	menu1.setStyle('borderColor', '#eefdfc');
	menu1.setStyle('headerStyleName', 'accheader');
	menu1.resizeToContent = true;
	menu1.setStyle('headerHeight', '25');
				
	for each(var s:XML in menuxml.item){
		var can1:Canvas = new Canvas();
		//can1.percentHeight = 100;
		can1.percentWidth = 100;
		var list1:List = new List();
		list1.percentWidth = 100;
		//list1.percentHeight = 100;
		list1.setStyle('backgroundColor','#eefdfc');
		list1.setStyle('borderColor','#eefdfc');
		list1.setStyle('textAlign','center');
		can1.label = s.@label;
		//list1.labelField = s.@label;		
		list1.dataProvider = s.item;
		list1.itemRenderer = new ClassFactory(listRenderer);
		list1.addEventListener(ListEvent.ITEM_CLICK, showsour);
		
		trace('s.item.length:');
		var sil:Number = s.item.length(); 
		trace(sil);
		list1.height = sil*35;
		can1.height = sil*35;
		
		can1.addChild(list1);
		menu1.addChild(can1);
		
	}
	
	menu1.selectedIndex = menu1.numChildren-1;
	menu1.getChildAt(menu1.numChildren-1).height = 0;
	menu1.addEventListener(MouseEvent.CLICK, function (e:MouseEvent):void{
				(menu1.getChildAt(menu1.numChildren-1) as Canvas).percentHeight = 100;
				//menu1.height = menu1.selectedChild.height;
				trace("menu1.selectedChild.height:");
				trace(menu1.selectedChild.height);
				//trace(menu1.getChildAt(menu1.selectedIndex).name);
				//var tmpc:Canvas = menu1.getChildAt(menu1.selectedIndex) as Canvas;
				//trace(tmpc.getChildAt(0).name);
				//var tmpl:List = tmpc.getChildAt(0) as List;
				//trace(tmpl.rowCount);
				//trace(tmpl.rowHeight);
				//tmpc.height = (tmpl.rowCount/3)*tmpl.rowHeight;
				//trace(s.item.length);
			}
		);
	
			
	left_box.removeAllChildren();
	left_box.addChild(menu1);
}


/**
 * 选择素材
 */
public function showsour(event:ListEvent):void{
	//selectedItem:Object = List(event.target).selectedItem;
	//trace(event.itemRenderer.data.@id);
	var param:Object = {id:event.itemRenderer.data.@id};
	this.SendHttpService(this.URLSourceRPC, param, this.SourceRPCResult, 'Source');
}

/**
 * 转到tips页面
 */
public function gototips(event:Event):void{
	navigateToURL(new URLRequest('tips.php'), '_self');
}

/**
 * 保存场景，边框，日历
 */
public function save():void{
	
}

/**
 * 边框预览窗口
 */
public function ShowBorderDialog(iSource:Object, bSource:Object):void{
	// 这里第三个参数 true 表示: 弹出myPanel窗口后, 是否可以对父级操作, 值为true时, 就会产生背景模糊的效果, 并不能对父级的东西进行操作
	var pop:viewBorder = viewBorder(PopUpManager.createPopUp(this, viewBorder, true)); 
	pop.mapp = this;
	pop.cbFunc = this.popBorderFun;
	pop.valb = bSource;
	pop.vali = iSource;
}
/**
 * 边框预览窗口回调
 * picdata ByteArray
 * value:Object {imgname:String, w:Number, h:Number }
 */
private function popBorderFun(value:Object):void{
	//addProcessBar(); //载入进度条。目前没用。
	//trace('In popBorderFun ');
	//trace(value.imgname);
	var byteArray:ByteArray = value.ba;
	
	var header:URLRequestHeader = new URLRequestHeader("Content-type", "applicaton/octet-stream");
	var tmpurl:String = new String();
	tmpurl = "pngborder.php";
	var savePNG:URLRequest = new URLRequest(tmpurl);
	savePNG.requestHeaders.push(header);
	savePNG.method = URLRequestMethod.POST;
	savePNG.data = byteArray;
	var l:URLLoader = new URLLoader();  
	l.load(savePNG);
	//var han:Object = this;
	l.addEventListener(Event.COMPLETE, function (e:Event):void{PngBorderComplete(e, value.imgname);}); //新图片地址，程序中图片的name
	//canvas1(loadedSM.application).setMainImage(value);
}
//
private function PngBorderComplete(e:Event, imgname:String):void{
	var l:URLLoader = URLLoader(e.target);  
	//trace(l.data);
	var ld:XML = XML(l.data);
	if(ld.info.result == 'OK'){
		canvas1(loadedSM.application).changeImage(ld.info.file, imgname); //替换图片
	}
	//removeProcessBar();
}

/**
 * 设置画布大小
 */
public function ShowSetCanvaSize(sHandle:Object):void{
	var pop3:setCanvasize = setCanvasize(PopUpManager.createPopUp(this, setCanvasize, true)); 
	pop3.mapp = this;
	pop3.cbFunc = this.popSetCanvas;
	pop3.val = sHandle;
}
/**
 * 设置画布大小回调
 */
private function popSetCanvas(value:Object):void{
	canvas1(loadedSM.application).setCanvaSize(value);
	//缩放到一定比例
	if(value.width>500 || value.height>500){
		zoom.zoomWidthFrom = 0.01;
		zoom.zoomHeightFrom = 0.01;
		zoom.zoomWidthTo = 0.5;
		zoom.zoomHeightTo = 0.5;
		hsz.value = 50;
		zoom.play();
		this.hsz_zoomHfrom = 0.5;
		this.hsz_zoomWfrom = 0.5;
	}else{
		zoom.zoomWidthFrom = 0.01;
		zoom.zoomHeightFrom = 0.01;
		zoom.zoomWidthTo = 1;
		zoom.zoomHeightTo = 1;
		hsz.value = 100;
		zoom.play();
	}
	this.isOPT = 4;
	//状态栏画布大小信息
	this.default_w = value.width;
	this.default_h = value.height;
	this.default_name = value.cl;
}

//设置画布大小2
private function SetCanvas2(value:Object):void{
	canvas1(loadedSM.application).setCanvaSize(value);
	//缩放到一定比例
	var zratio:Number = 1; //缩放值，默认为100%。
	var xw:Number = pa1.width/value.width;//trace('xw'+xw);
	var xh:Number = pa1.height/value.height;//trace('xh'+xh);
	if( value.width > pa1.width ){
		//宽超出范围
		zratio = xw;
	}else if(value.height>pa1.height){
		// 未超出，不缩放，以原始大小显示。
		zratio = xh;
	}
	//trace(zratio);
	var zt:Number = Math.floor(zratio*100);
	
	zoom.zoomHeightTo = zt/100;
	zoom.zoomWidthTo = zt/100;
	hsz.value = zt;
	zoom.play();
	
	//this.isOPT = 4;
	//状态栏画布大小信息
	this.default_w = value.width;
	this.default_h = value.height;
	this.default_name = value.cl;
}




/**
 * 场景应用
 */
public function SetStage(sHandle:Object):void{
	var pop4:stages = stages(PopUpManager.createPopUp(this, stages, true)); 
	pop4.mapp = this;
	pop4.cbFunc = this.popSetStage;
	pop4.val = sHandle;
}
/**
 * 回调 场景应用
 */
private function popSetStage(value:Object):void{
	//合成场景时显示进度条
	this.ishowml = true;
	//this.addChild(this.mling);
	//
	//canvas1(loadedSM.application).stages(value);
	this.SendHttpService(this.URLFinishStageRPC, value, this.FinishStageRPCResult, '合成场景');
	
}


/**
 * 显示原始照片，即没有被编辑过的，照片。也可能是重新选择后的照片。
 * 消除调节属性
 */
public function showOriginal():void{
	//var popw:originalPhoto = originalPhoto(PopUpManager.createPopUp(this, originalPhoto, true));
	//popw.mapp = this;
	//popw.cbFunc = this.popshowOriginal;
	//popw.val = this.oriPhoto;
	canvas1(loadedSM.application).effReset();
}

/**
 * 从canvas里执行显示对比窗口
 */
public function showTwoWin1():void{
	canvas1(loadedSM.application).showTwoWin_c();
}

/**
 * 显示对比窗口
 */
public function showTwoWin(value:Object):void{
	var popw:twoWin = twoWin(PopUpManager.createPopUp(this, twoWin, true));
	popw.mapp = this;
	//popw.cbFunc = this.popshowOriginal;
	popw.val1 = this.oriPhoto;
	popw.val2 = value;
	//trace(value);
}

//从canvas里执行显示对比窗口
public function showPreview1():void{
	canvas1(loadedSM.application).showPreview_c();
}

/**
 * 显示预览窗口
 */
public function showPreview(value:Bitmap):void{
	var popw:preview = preview(PopUpManager.createPopUp(this, preview, true));
	popw.mapp = this;
	//popw.cbFunc = this.popshowOriginal;
	popw.val = value;
}



/**
 * save1()，直接将图片移到maked.
 * 场景，边框，日历
 */
public function save1(value:String):void{
	//图片都已经生成好了。只要保存就行。
	var tmp:Number = 0;
	if(this.click_obj_1_string == '1'){
		tmp = this.click_obj_1.idx;
	}
	var param:Object = {url:value, idx:tmp};
	SendHttpService(URLSave1, param, getSave1Result, '保存设计');
	
	
}

//删除我的照片
public function delMyPhoto(value:Object):void{
	//trace(value);
	var param:Object = {id:value};
	this.SendHttpService(this.URLDelMyPhotoRPC, param, this.DelMyPhotoRPCResult, '删除照片');
	
}

//删除完成的照片
public function delMyMaked(value:Object):void{
	//trace(value);
	var param:Object = {id:value};
	this.SendHttpService(this.URLDelMyMakedRPC, param, this.DelMyMakedRPCResult, '删除照片');
	
}
//显示用户的所有作品,单独页面，相册
public function gogallery():void{
	navigateToURL(new URLRequest('gallery.php'), '_blank');
}

//加载英文字体完成
public function loadFontComplete(e:Event):void{
	try{
        Font.registerFont( e.target.content["BakerScript"] );
        Font.registerFont( e.target.content["comsc"] );
        Font.registerFont( e.target.content["daniel"] );
        Font.registerFont( e.target.content["EasyStreetEPS"] );
        Font.registerFont( e.target.content["Filxgirl"] );
        Font.registerFont( e.target.content["gothice"] );
     }catch(er:Error){
        trace(er.toString());
        Alert.show('系统忙，请稍后再试', '提示');
     }finally{
     	//删除初始化屏幕 2
     	//this.initTotal -= 1;
     	//trace(this.initTotal);
		//if(this.initTotal == 0){
		//	this.removeChild(this.mling);
		//}
     }
}

//加载中文字体
public function loadHanFontComplete(e:Event):void{
	try{
        Font.registerFont( e.target.content["hanfont1"] );
        Font.registerFont( e.target.content["hanfont2"] );
        Font.registerFont( e.target.content["hanfont6"] );
        Font.registerFont( e.target.content["hanfont4"] );
        Font.registerFont( e.target.content["hanfont5"] );
        
     }catch(er:Error){
        trace(er.toString());
        Alert.show('系统忙，请稍后再试', '提示');
     }finally{
     	//删除初始化屏幕 3
     	//this.initTotal -= 1;
		//trace(this.initTotal);
		//if(this.initTotal == 0){
		//	this.removeChild(this.mling);
		//}
     }
}

//删除场景元素
public function dele():void{
	canvas1(loadedSM.application).delit();
}

//显示等待进度条
public function addProcessBar():void{
	//mling.mylbild();
	this.addChild(this.mling);
	this.ishowml = true;
}

//删除等待进度条
public function removeProcessBar():void{
	if(this.ishowml){
		this.removeChild(this.mling);
		this.ishowml = false;
	}
}

//将素材置下
public function put_down():void{
	canvas1(loadedSM.application).doputdown();
}

//将素材置上
public function put_up():void{
	canvas1(loadedSM.application).doputup();
}

//hota 的鼠标按下事件
private function mypmouseDownHandler(event:MouseEvent):void{  
	//Alert.show(event.target.toString());
	//var dragInitiator:Object = event.currentTarget;
    //var ds:DragSource = new DragSource();
    //ds.addData(dragInitiator, "myRule");
    //DragManager.doDrag(dragInitiator, ds, event);
}

//一个照片裁切窗口
public function showCutWindow(url:String, w:Number, h:Number):void{
	var pop:showCropWin = showCropWin(PopUpManager.createPopUp(this, showCropWin, true));
	pop.mapp = this;
	//popw.cbFunc = this.popshowOriginal;
	pop.imgobj = {imgurl:url, width:w, heith:h};
	
}

// go left
private function _gleft(delta:int = 10):void{
	if(id_mymaked.horizontalScrollPosition < id_mymaked.maxHorizontalScrollPosition){
		id_mymaked.horizontalScrollPosition += 1;
	}
}

// go right
private function _gright(delta:int = 10):void{
	if(id_mymaked.horizontalScrollPosition > 0){
		id_mymaked.horizontalScrollPosition -= 1;
	}
}

private function showVersion(value:XML):void{
	var lastupdate:String = value.lastupdate;
	var version:String = value.version;
	var subver:String = value.subver;
	var canver:Canvas = new Canvas();
	canver.height = 15;
	canver.width = 400;
	
	var canver_l1:Label = new Label();
	canver_l1.text = "Version:"+version+" u"+subver+"    LastUpdate: "+lastupdate;
	canver_l1.setStyle("fontSize","10");
	canver_l1.setStyle("color", "#cccccc");
	canver_l1.addEventListener(MouseEvent.MOUSE_OVER, _boldFont);
	canver_l1.addEventListener(MouseEvent.MOUSE_OUT, _normalFont);
	canver_l1.addEventListener(MouseEvent.CLICK, _showLog);
	//var canver_l2:Label = new Label();
	//canver_l2.text = "LastUpdate: "+lastupdate;
	//canver_l2.setStyle("fontSize","8");
	//canver_l2.y = 20;
	canver.addChild(canver_l1);
	//canver.addChild(canver_l2);
	
	addChild(canver);
	canver.y = this.height - 15;
}


/* 底部版本号字体颜色 鼠标事件 */
private function _boldFont(e:MouseEvent):void{
	e.currentTarget.setStyle("color", "#333333");
}
private function _normalFont(e:MouseEvent):void{
	e.currentTarget.setStyle("color", "#cccccc");
}

/* 转到相册页面 */
private function _showLog(e:MouseEvent):void{
	navigateToURL(new URLRequest('version.php'), '_blank');
}

/**
 * 显示属性对话框架
 */
public function showCtrBar(value:Object):void{
	ctrbar.visible = true;
	//trace('ctrbar true');
	var t:Object = canvas1(loadedSM.application).getPorpe();
	//trace(t);
	if(t != null){
		imib_o_r.value = t._rotate;
		imib_o_w.value = t._width;
		imib_o_h.value = t._height;
		imib_o_x.value = t._x;
		//imib_o_y.value = t._y;
		
	}
}

/**
 * 隐藏属性对话框
 */
public function hiddenCtrBar():void{
	ctrbar.visible = false;
	//trace('ctrbar false');
}

/**
 * 修改热区内图像属性
 */
private function doimibchange(e:NumericStepperEvent,val:Object):void{
	//trace(e.value);
	canvas1(loadedSM.application)._do_imib_change(e.value,val);
}

/**
 * 修改热区属性
 */
private function doimibfchange(val:String):void{
	canvas1(loadedSM.application)._do_imib_f_change(val);
	
}

/**
 * 转到购物车
 */
public function addtocar():void{
	//trace(this.shopid);
	navigateToURL(new URLRequest('../addtocar.php?id='+this.shopid), '_self');
}

/* 设置热区内图片的X */
public function _setHotImgX(e:MouseEvent):void{
	this._hot_is_mouse_down = true;
	trace(this.mouseX);
}

//X上的鼠标移动
public function _setHotImgXMove(e:MouseEvent):void{
	if(this._hot_is_mouse_down){
		trace(e.stageX);
	}
}
// 鼠标松开
public function _setHotImgXUp(e:MouseEvent):void{
	this._hot_is_mouse_down = false;
}

/**
 * 用户选择照片
 * 
 */
private function SelectPhotos():void{
	fileList.browse([imgfilter]);
	

}

private function fileActivate(e:Event):void{

}


/**
 * 文件选择
 *
 */
private function fileSelect(e:Event):void{
    
    
    var files:FileReferenceList = FileReferenceList(e.target);
    selectedFileArray = files.fileList;
    this.countFile = this.selectedFileArray.length;
    this.countDo = 0;
    this.uploadFileSingle(this.countDo);
    
}

private function fileDeactivate(e:Event):void{

}

private function fileCancel(e:Event):void{

}
private function uploadFileSingle(cou:Number):void{
	var request:URLRequest = new URLRequest("upload.php");
    var variables:URLVariables = new URLVariables();
    variables.userid = this.userid;
    request.data=variables;
	request.method=URLRequestMethod.POST;
	
	//this.countFile = this.selectedFileArray.length;
    //for(var i:uint=0; i<this.selectedFileArray.length; i++){
    	var file:FileReference;
    	file = FileReference(this.selectedFileArray[cou]);
    	//Alert.show(file.name);
        file.addEventListener(Event.COMPLETE, completeHandler);
        file.addEventListener(IOErrorEvent.IO_ERROR, fileErrorHandle);
        file.addEventListener(ProgressEvent.PROGRESS, progressHandle);
        file.addEventListener(HTTPStatusEvent.HTTP_STATUS, fileStatus);
        file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);

        try{
        	file.upload(request);
        }catch (error:Error){
            trace("Unable to upload files.");
            //trace(error.message);
            Alert.show('上传失败','错误');
        }
    //}
}

/**
 * FileReference的complete事件
 */
private function completeHandler(e:Event):void{
       
}

/* UPLOAD_COMPLETE_DATA */
private function uploadCompleteDataHandler(e:DataEvent):void{
	trace('上传成功一个');
	trace(e.data);
	var backstr:String = e.data;
	//Alert.show(backstr);
	//if(backstr.length == 2){
		this.countDo++;
		//Alert.show(this.countDo.toString());
    	if(this.countDo < this.selectedFileArray.length){
    		this.uploadFileSingle(this.countDo);
    	}
    	this.countFile--;
    	if(this.countFile==0){
    		this.sys_msg = '上传完成';
    		//this.getGoodsPhotos();
    	}
 	//}
    this.getUserPhotos();
}

/**
 * 文件IO错误
 */
private function fileErrorHandle(e:ErrorEvent):void{
	//trace(e.toString());
	Alert.show("系统错误！上传失败。","错误");
}



private function progressHandle(e:ProgressEvent):void{
	//var prog:Number = e.bytesLoaded/e.bytesTotal;
	//this.mp.goprocess(prog);
}

/* fileStatus */
private function fileStatus(e:HTTPStatusEvent):void{
	//trace(e.toString());
}



private function getUserPhotos():void{
	this.SendHttpService(this.url_MyPhotoRPC, null, this.getMyPhotoRPCResult, 'Photo');
}


public function setTextValueBack(val:Array):void{
	this.ltextfont.selectedItem = val[0];
	this.ltextSize.text = val[1];
	this.ltextcolor.selectedColor = val[2];
	this.textctrbar.visible = true;
}

public function hiddentextctrbar():void{
	this.textctrbar.visible = false;
}


public function setTextLable():void{
	//ltextcolor.value,ltextfont.selectedItem,ltextSize.value
	var ar:Array = [ltextfont.selectedItem, ltextSize.text, ltextcolor.value];
	canvas1(loadedSM.application)._set_label_text(ar);
}




